__author__ = 'tyrion'

from Base import BaseCreateMonitors
from conf.gloal import *
from src.common.tools.salt import *
from src.common.tools.httpd import *

class NginxMonitors(BaseCreateMonitors):
    def __init__(self, conf):
        print("info: create nginx monitors init")
        self.hostName = conf.get("hostname", "error: not found host name")
        self.aliasName = conf.get("hostname", "error: not found host name")
        self.groupName = "flexweb"
        self.floder = "flexweb"
        self.ip = conf.get("ip", "error: not found host ip")
        self.key = "%s%s" % (conf.get("place"), conf.get("env"))

        return

    def CreateMonitors(self):
        print("info: create nginx monitors")

        return

    def AddToMonitorSvr(self):
        print("info: add nginx monitors to monitor svr")
        cmdI = "cmk -I %s " %(self.hostName)
        cmdIRet = ExecuteCommand(REAL_ICINGA_SERVER.get(self.key),  cmdI, saltMaster = REAL_SALT_MATER.get(self.key))
        cmdIErrMsg = "cmk -I failed"
        cmdIStatus = AnalyseResult(cmdIRet, cmdIErrMsg, REAL_ICINGA_SERVER.get(self.key))
        if cmdIStatus == "false":
            return

        cmdO = "cmk -O"
        cmdORet = ExecuteCommand(REAL_ICINGA_SERVER.get(self.key), cmdO, saltMaster = REAL_SALT_MATER.get(self.key))
        cmdOErrMsg = "cmk - O failed"
        cmdOStatus = AnalyseResult(cmdORet, cmdOErrMsg, REAL_ICINGA_SERVER.get(self.key))
        if cmdOStatus == "false":
            return
        return 200


    def CreateMachine(self):
        print("info: create nginx machine")
        url = '''
                http://%s:%s/check_mk/wato.py?filled_in=edithost&_username=icinga_nd&_secret=1234567890&_transid=-1&host=%s&_change_contactgroups=on&contactgroups_%s=on&contactgroups_use=on&_change_alias=on&attr_alias=%s&_change_ipaddress=on&attr_ipaddress=%s&attr_snmp_community=&attr_display_snmp_community=0&parents_0=&attr_tag_agent=cmk-agent|tcp&_change_tag_criticality=on&attr_tag_criticality=prod&_change_tag_networking=on&attr_tag_networking=lan&services=Save%%20%%26%%20go%%20to%%20Services&folder=%s&mode=newhost
                ''' % (REAL_ICINGA_SERVER.get(self.key), REAL_ICINGA_SERVER_PORT.get(self.key), self.hostName, self.groupName, self.aliasName, self.ip, self.floder)

        print( url)
        ret = GetHttpRequest(url)

        return ret[1]

    def DeleteMachine(self):
        print("info: delete mongo machine")
        url = "http://%s:%s/check_mk/wato.py?mode=folder&_username=icinga_nd&_secret=1234567890&_delete_host=%s&_transid=-1&folder=%s&filled_in=confirm&_do_confirm=Yes!"%(REAL_ICINGA_SERVER.get(self.key), REAL_ICINGA_SERVER_PORT.get(self.key), self.hostName, self.floder)
        print(url)
        ret = GetHttpRequest(url)
        return ret[1]

    def GetKey(self):
        return  self.key


PLUGINS["Nginx"] = "NginxMonitors"


if __name__ == "__main__":

    conf = {"hostname": "lzd-test-02", "ip":"172.24.133.40"}

    nginx =  NginxMonitors(conf)
    # nginx.CreateMachine()
    # nginx.AddToMonitorSvr()
    nginx.DeleteMachine()
    nginx.ReloadConf()



